package com.mstore.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mstore.pojo.Account;
import com.mstore.pojo.Question;
import com.mstore.pojo.Reply;
import com.mstore.pojo.WxReplyItem;
import com.mstore.service.QuestionService;
import com.mstore.service.ReplyService;
import com.mstore.service.WxReplyItemService;
import com.mstore.util.ImageUpload;
import com.opensymphony.xwork2.ActionContext;

/**
 * 用户上传多图文消息
 */
public class WxReplyItemAction extends BaseAction {

	private static final long serialVersionUID = -5073203624230313742L;

	private File[] upload;

	private String[] uploadContentType;

	private String[] uploadFileName;

	private Map<String, Object> session = ActionContext.getContext()
			.getSession();

	private String result;

	private WxReplyItemService wxReplyItemService;

	private ReplyService replyService;

	private List<Reply> replys = new ArrayList<Reply>();

	private List<WxReplyItem> wxReplyItems = new ArrayList<WxReplyItem>();

	private String data;

	private Reply reply;

	private String wxId;

	private QuestionService questionService;

	/**
	 * 多图文信息的保存
	 */
	public String readImageText() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;
		reply = new Reply();
		String[] datas = data.split("\\|");

		for (int i = 0; i < datas.length - 1; i++) {
			// System.out.println("datas=" + datas[i]);
			WxReplyItem item = new WxReplyItem();
			String[] dataStr = datas[i].split("##,");

			if (i == 0) {
				item.setDescription(dataStr[0]);
				item.setTitle(dataStr[1]);
				reply.setTitle(dataStr[1]);
				item.setUrl(dataStr[2]);
				item.setPicurl(dataStr[3]);
			}
			else {
				for (int j = 0; j < dataStr.length; j++) {

					switch (j) {
						case 0:
							String a = dataStr[0].substring(0, 1);

							if (a.equals(",")) {
								String dataStr1 = dataStr[0].substring(1,
										dataStr[0].length());
								item.setDescription(dataStr1);
							}
							else {
								item.setDescription(dataStr[0]);
							}
							break;
						case 1:
							item.setTitle(dataStr[1]);
							break;
						case 2:
							item.setUrl(dataStr[2]);
							break;
						case 3:
							item.setPicurl(dataStr[3]);
							break;
						default:
							break;
					}
				}
			}

			wxReplyItems.add(item);

		}
		reply.setCreateTime(getCreateTime());
		reply.setEaId(account.getId());
		reply.setType(3);
		replyService.saveReply(reply);
		for (int k = 0; k < wxReplyItems.size(); k++) {
			wxReplyItems.get(k).setReplyId(reply.getId());
		}
		for (int k = 0; k < wxReplyItems.size(); k++) {

			wxReplyItemService.saveWxReplyItem(wxReplyItems.get(k));
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("temp", "图文消息保存成功！");
		result = super.mapTojson(map);

		return "imageUpload";

	}

	/**
	 * 上传图片的保存
	 */
	public String uploadImage() {
		try {
			String nowpath = ImageUpload.getUploadUrl();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
			String ymd = sdf.format(new Date());
			nowpath += ymd + "/";

			File[] files = getUpload();
			for (int i = 0; i < files.length; i++) {
				File uploadPath = new File(nowpath); // 上传文件目录
				if (!uploadPath.exists()) {
					uploadPath.mkdirs();
				}
				if (files[i].length() == 0) {
					continue;
				}
				// 用时间戳重命名上传文件名
				String suffix = uploadFileName[i].substring(
						uploadFileName[i].lastIndexOf("."),
						uploadFileName[i].length());
				String fileName = "wx" + System.currentTimeMillis() + suffix;
				FileOutputStream fos = new FileOutputStream(uploadPath + "/"
						+ fileName);
				// 开始上传
				FileInputStream fis = new FileInputStream(files[i]);
				byte[] buffer = new byte[1024];
				int len = 0;
				while ((len = fis.read(buffer)) > 0) {
					fos.write(buffer, 0, len);
				}
				Map<String, Object> map = new HashMap<String, Object>();
				String tempPath = ImageUpload.getDownloadUrl() + ymd + "/"
						+ fileName;
				map.put("temp", tempPath);
				result = super.mapTojson(map);
			}
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}

		return "image";
	}

	/**
	 * 
	 * 查询企业多图文信息
	 */
	public String watchReplyItem() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		Account account = (Account) object;

		reply = new Reply();
		reply.setEaId(account.getId());

		replys = wxReplyItemService.watchReplyItem(reply);

		return "watchReplyItem";
	}

	/**
	 * 
	 * 删除图文信息
	 */
	public String delReplyItem() {
		Object object = session.get("account");
		if (object == null) {
			result = ERROR_TIP;
			return "error";
		}
		Account account = (Account) object;
		Question question = new Question();
		question.setEaId(account.getId());
		question.setRelyId(reply.getId());

		List<Question> questions = questionService
				.findQuestionsByReplyId(question);
		for (Question ques : questions) {
			questionService.delQuestion(ques);
		}

		wxReplyItemService.delReplyItem(reply);
		replyService.delReply(reply);
		return "delSuccess";
	}

	/**
	 * 
	 * 进入修改页面的查询
	 */
	public String selectReplyAndWxReplyItem() {
		wxReplyItems = wxReplyItemService.selectByReplyId(reply);
		return "selectSuccess";
	}

	/**
	 * 
	 * 修改多图文
	 */
	public String update() {
		Object object = session.get("account");
		if (object == null) {
			result = "系统异常，请重新登陆！";
			return "error";
		}
		reply = new Reply();
		String[] datas = data.split("\\|");
		for (int i = 0; i < datas.length; i++) {
			WxReplyItem item = new WxReplyItem();
			String[] dataStr = datas[i].split("##,");
			if (i == 0) {
				item.setDescription(dataStr[0]);
				item.setTitle(dataStr[1]);
				reply.setTitle(dataStr[1]);
				reply.setId(Long.parseLong(dataStr[5]));
				item.setUrl(dataStr[2]);
				item.setPicurl(dataStr[3]);
				item.setId(Long.parseLong(dataStr[4]));
			}
			else {
				for (int j = 0; j < dataStr.length; j++) {
					switch (j) {
						case 0:

							String a = dataStr[0].substring(0, 1);
							if (a.equals(",")) {
								String dataStr1 = dataStr[0].substring(1,
										dataStr[0].length());
								item.setDescription(dataStr1);
							}
							else {
								item.setDescription(dataStr[0]);
							}
							break;
						case 1:

							item.setTitle(dataStr[1]);
							break;
						case 2:

							item.setUrl(dataStr[2]);
							break;
						case 3:

							item.setPicurl(dataStr[3]);
							break;
						case 4:
							if (!dataStr[4].equals(""))
								item.setId(Long.parseLong(dataStr[4]));

						default:
							break;
					}
				}
			}
			wxReplyItems.add(item);

		}

		replyService.update(reply);

		for (int k = 0; k < wxReplyItems.size(); k++) {
			if (wxReplyItems.get(k).getId() == 0) {
				wxReplyItems.get(k).setReplyId(reply.getId());
				wxReplyItemService.saveWxReplyItem(wxReplyItems.get(k));
			}
			wxReplyItemService.updateWxReplyItem(wxReplyItems.get(k));
		}

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("temp", "图文消息修改成功！");
		result = super.mapTojson(map);

		return "updateWxReplyItem";
	}

	/**
	 * 
	 * 在修改页面中删除单个图文
	 */

	public String deleteById() {
		WxReplyItem item = new WxReplyItem();
		item.setId(Long.parseLong(wxId));
		wxReplyItemService.deleteReplyItem(item);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("temp", "图文消息删除成功！");
		result = super.mapTojson(map);
		return "deleteSuccess";
	}
	
	/**
	 * 跳转到会员中心编辑页面
	 */
	public String initTextureMember(){
		return "toTextureMember";
	}  

	public File[] getUpload() {
		return upload;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}

	public ReplyService getReplyService() {
		return replyService;
	}

	public void setReplyService(ReplyService replyService) {
		this.replyService = replyService;
	}

	public String[] getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String[] uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public WxReplyItemService getWxReplyItemService() {
		return wxReplyItemService;
	}

	public void setWxReplyItemService(WxReplyItemService wxReplyItemService) {
		this.wxReplyItemService = wxReplyItemService;
	}

	public List<Reply> getReplys() {
		return replys;
	}

	public void setReplys(List<Reply> replys) {
		this.replys = replys;
	}

	public List<WxReplyItem> getWxReplyItems() {
		return wxReplyItems;
	}

	public void setWxReplyItems(List<WxReplyItem> wxReplyItems) {
		this.wxReplyItems = wxReplyItems;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}

	public String getWxId() {
		return wxId;
	}

	public void setWxId(String wxId) {
		this.wxId = wxId;
	}

	public QuestionService getQuestionService() {
		return questionService;
	}

	public void setQuestionService(QuestionService questionService) {
		this.questionService = questionService;
	}

}
